package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.SliderUI;
import javax.xml.ws.Response;
import java.util.List;

/**
 * @package com.leyou.item.controller
 * @description:
 * @author: 86157
 * @date 2019/6/26 21:50
 * @version: V1.0
 */
@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) boolean saleable,
            @RequestParam(value = "key", required = false) String key) {

        return ResponseEntity.ok(goodsService.querySpuByPage(page, rows, saleable, key));

    }


    /**
     * 新增商品
     *
     * @param spuDTO
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改商品前下架商品
     *
     * @param
     * @return
     */
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSaleableGoods(@RequestParam("id") Long id,
                                                    @RequestParam("saleable") Boolean saleable) {
        goodsService.updateSaleable(id, saleable);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuid查询spuDetail
     *
     * @param
     * @return
     */
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetail(@RequestParam("id") Long id) {

        return ResponseEntity.ok(goodsService.querySpuDetail(id));
    }

    /**
     * 根据spuid查询sku
     *
     * @param
     * @return
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySku(@RequestParam("id") Long id) {

        return ResponseEntity.ok(goodsService.querySku(id));
    }

    /**
     * 修改商品
     *
     * @param spuDTO
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}